public class eightqueen {

public static int order=0;
    public static void show(int arr[][]) {
        System.out.println("第"+ ++order+"种：");
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                System.out.print(arr[j][i] == 1 ? "Q" : "□");
            }
            System.out.println();
        }
    }

    public static boolean isValid(int arr[][], int column, int row) {
        //同行检测[列][行]
        for (int i=column;i>=0;i--) {
            if (arr[i][row]==1)return false;
        }
        //左斜右
        for (int i=column-1,j=row-1;i>=0&&j>=0;i--,j--){
            if (arr[i][j]==1)return false;
        }
        //右斜左
        for (int i=column-1;i>=0&&(column+row-i)<8;i--) {
            if (arr[i][column+row-i]==1)return false;
        }
        return true;
    }

    public static void queenTest(int arr[][], int column) {
        if (column > 7) {
            show(arr);
            return;
        }

        for (int j = 0; j < 8; j++) {
            if (isValid(arr, column, j)) {
                arr[column][j] = 1;
                queenTest(arr, column + 1);
                arr[column][j] = 0;
            }
        }
    }

    public static void main(String[] args) {
        int[][] arr = new int[8][8];

        queenTest(arr, 0);

    }
}
